// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.metadata;

import java.util.List;

import org.dengues.core.metadata.MetadataHelper;
import org.dengues.core.metadata.MetadataTable;
import org.dengues.core.metadata.ui.MetadataTableViewer;
import org.dengues.model.database.DBColumn;
import org.dengues.model.database.DBTable;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * @since 2007-9-14 上午12:52:01
 */
public class MetadataDialog extends Dialog {

    private boolean canModifyName = false;

    public static String TITLE = "Config Table";

    private MetadataTableViewer viewer;

    private final MetadataTable metadataTable;

    /**
     * Qiang.Zhang.Adolf@gmail.com MetadataDialog constructor comment.
     * 
     * @param parentShell
     * @param table
     */
    public MetadataDialog(Shell parentShell, DBTable table) {
        this(parentShell, MetadataHelper.convertMetadataTable(table));
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com MetadataDialog constructor comment.
     * 
     * @param parentShell
     * @param table
     */
    public MetadataDialog(Shell parentShell, MetadataTable metadataTable) {
        super(parentShell);
        setShellStyle(SWT.RESIZE | SWT.APPLICATION_MODAL | SWT.MAX | SWT.MIN | SWT.DIALOG_TRIM);
        this.metadataTable = metadataTable.clone();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
     */
    @Override
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setSize(600, 400);
        newShell.setText(TITLE);
        newShell.setLocation(Display.getCurrent().getActiveShell().toDisplay(100, 100));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected Control createDialogArea(Composite parent) {
        Composite container = (Composite) super.createDialogArea(parent);
        createTable(container);
        Composite toolBar = new Composite(container, SWT.NONE);
        toolBar.setLayout(new GridLayout());
        toolBar.setLayoutData(new GridData(GridData.FILL_BOTH));
        return container;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createTable".
     * 
     * @param container
     */
    private void createTable(Composite container) {
        viewer = new MetadataTableViewer(container, metadataTable);
        viewer.setCanModifyName(isCanModifyName());
        viewer.initTableInput();
    }

    private List<DBColumn> dbColumns;

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#okPressed()
     */
    @Override
    protected void okPressed() {
        dbColumns = MetadataHelper.convertDBTable(viewer.getMetadataTable());
        super.okPressed();
    }

    /**
     * Getter for dbColumns.
     * 
     * @return the dbColumns
     */
    public List<DBColumn> getDbColumns() {
        return this.dbColumns;
    }

    /**
     * Getter for metadataTable.
     * 
     * @return the metadataTable
     */
    public MetadataTable getMetadataTable() {
        return this.metadataTable.clone();
    }

    /**
     * Getter for canModifyName.
     * 
     * @return the canModifyName
     */
    public boolean isCanModifyName() {
        return this.canModifyName;
    }

    /**
     * Sets the canModifyName.
     * 
     * @param canModifyName the canModifyName to set
     */
    public void setCanModifyName(boolean canModifyName) {
        this.canModifyName = canModifyName;
    }
}
